package com.he.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.he.pojo.Mail;
import com.he.pojo.MailBox;
import com.he.pojo.MailBoxExample;
import com.he.pojo.Trash;
import com.he.pojo.TrashExample;
import com.he.pojo.User;
import com.he.service.mail.MailServiceI;
import com.he.service.mailbox.MailBoxServiceI;
import com.he.service.trash.TrashServiceI;
import com.he.service.user.UserServiceI;

@Controller
@RequestMapping("mail")
public class MailBoxController {
	//获取不同的example，key为1代表草稿箱，key为2代表发信箱，key为3代表收信箱，key为4代表回收站
	public MailBoxExample getBox(HttpServletRequest request,int key) {
		HttpSession session = request.getSession();
		User user =(User) session.getAttribute("user");
		MailBoxExample example=new MailBoxExample();
		switch (key) {
			case 1:
				example.createCriteria().andSendIdEqualTo(user.getId()).andDraftsEqualTo((byte)1);
				example.setOrderByClause("mail_date desc");
				break;
			case 2:
				example.createCriteria().andSendIdEqualTo(user.getId()).andSendMailEqualTo((byte)1);
				example.setOrderByClause("mail_date desc");
				break;
			case 3:
				example.createCriteria().andReceiveIdEqualTo(user.getId()).andInboxEqualTo((byte)1);
				example.setOrderByClause("mail_date desc");
				break;
			default:
				break;
		}
		return example;
	}
	//为前端传递的mailbox参数赋值
	public MailBox result(MailBox mailBox,List<MailBox> list,String url) {
		PageInfo<MailBox> pageInfo=new PageInfo<>(list);
		mailBox.setPages( pageInfo.getPages());
		mailBox.setTotal(pageInfo.getTotal());
		mailBox.setUrl(url);
		return mailBox;
	}
	@Autowired
	MailServiceI mailService;
	@Autowired
	MailBoxServiceI boxService;
	@Autowired
	TrashServiceI trashService;
	//草稿箱显示
	@RequestMapping("drafts")
	public String drafts(HttpServletRequest request,ModelMap map,MailBox mailBox) {
		//获取当前用户的信箱为草稿箱
		MailBoxExample example = getBox(request, 1);
		List<MailBox> drafts = boxService.getPageMailBox(example, mailBox);
		map.addAttribute("drafts", drafts);
		mailBox=result(mailBox, drafts, "drafts.do");
		map.addAttribute("mailBox", mailBox);
		return"drafts";
	}
	//发信箱显示
	@RequestMapping("sendMail")
	public String sendMail(HttpServletRequest request,ModelMap map,MailBox mailBox) {
		//获取当前用户的信箱为发信箱
		MailBoxExample example = getBox(request, 2);
		List<MailBox> sentMail = boxService.getPageMailBox(example, mailBox);
		map.addAttribute("sentMail", sentMail);
		mailBox=result(mailBox, sentMail, "sendMail.do");
		map.addAttribute("mailBox", mailBox);
		return"outbox";
	}
	//收信箱显示
	@RequestMapping("inbox")
	public String inbox(HttpServletRequest request,ModelMap map,MailBox mailBox) {
		//获取当前用户的信箱为收信箱
		MailBoxExample example = getBox(request, 3);
		List<MailBox> inbox = boxService.getPageMailBox(example, mailBox);
		map.addAttribute("inbox", inbox);
		mailBox=result(mailBox, inbox, "inbox.do");
		map.addAttribute("mailBox", mailBox);
		return"inbox";
	}
	//回收站显示
	@RequestMapping("trash")
	public String trash(HttpServletRequest request,ModelMap map,Trash trash) {
		//获取当前用户的信箱为回收站
		TrashExample example=new TrashExample();
		HttpSession session = request.getSession();
		User user =(User) session.getAttribute("user");
		example.createCriteria().andUserIdEqualTo(user.getId());
		example.setOrderByClause("mail_date desc");
		List<Trash> trashs = trashService.getPageTrashs(example, trash);
		map.addAttribute("trashs", trashs);
		PageInfo<Trash> pageInfo=new PageInfo<>(trashs);
		trash.setPages( pageInfo.getPages());
		trash.setTotal(pageInfo.getTotal());
		trash.setUrl("trash.do");
		map.addAttribute("trash", trash);
		return"recyclebin";
	}
	@Autowired
	UserServiceI userService;
	//跳转到写信页面
	@RequestMapping(value="writeMail",method=RequestMethod.GET)
	public String writeMailGet(ModelMap map,MailBox box) {
		User user=new User();
		user.setWithPage(0);
		List<User> users = userService.getUsers(null, user);
		String jsonUsers = JSON.toJSONString(users);
		map.addAttribute("jsonUsers", jsonUsers);
		return "writemail";
	}
	//发送信
	@RequestMapping(value="writeMail",method=RequestMethod.POST)
	public String writeMailPost(HttpServletRequest request,Mail mail,MailBox mailBox) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		mail.setSendId(user.getId());
		mail.setSendName(user.getUserName());
		mail.setReceiveName(userService.getUserById(mail.getReceiveId()).getUserName());
		mail.setMailDate(new Date());
		mailService.insert(mail, mailBox);
		return "redirect:sendMail.do";
	}
	//存为草稿
	@RequestMapping(value="save",method=RequestMethod.POST)
	public String save(HttpServletRequest request,Mail mail,MailBox mailBox) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		mail.setSendId(user.getId());
		mail.setSendName(user.getUserName());
		mail.setReceiveName(userService.getUserById(mail.getReceiveId()).getUserName());
		mail.setMailDate(new Date());
		mailService.saveDraft(mail, mailBox);
		return "redirect:drafts.do";
	}
	//显示信详情，有回复
	@RequestMapping(value="detail",method=RequestMethod.GET)
	public String detail(MailBox mailBox,ModelMap map) {
		Mail mail = mailService.getMailById(mailBox.getMailId());
		map.addAttribute("mail", mail);
		return "maildetail";
	}
	//显示信详情，没有回复
	@RequestMapping(value="detailnoreply",method=RequestMethod.GET)
	public String detailNoReply(MailBox mailBox,ModelMap map) {
		Mail mail = mailService.getMailById(mailBox.getMailId());
		map.addAttribute("mail", mail);
		return "maildetailnoreply";
	}
	//回复信
	@RequestMapping(value="reply",method=RequestMethod.POST)
	public String reply(HttpServletRequest request,Mail mail,MailBox mailBox) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		mail.setSendId(user.getId());
		mail.setSendName(user.getUserName());
		mail.setReceiveName(userService.getUserById(mail.getReceiveId()).getUserName());
		mail.setMailDate(new Date());
		if(mail.getMailInfo().length()>10) {
			mail.setMailTitle(mail.getMailInfo().substring(0, 10));
		}else {
			mail.setMailTitle(mail.getMailInfo());
		}
		mailService.insert(mail, mailBox);
		return "redirect:sendMail.do";
	}
	//草稿箱查看
	@RequestMapping(value="draftWriter",method=RequestMethod.GET)
	public String draftWriter(ModelMap map,MailBox box) {
		User user=new User();
		user.setWithPage(0);
		List<User> users = userService.getUsers(null, user);
		String jsonUsers = JSON.toJSONString(users);
		map.addAttribute("jsonUsers", jsonUsers);
		Mail mail=mailService.getMailById(box.getMailId());
		map.addAttribute("mail", mail);
		return "draftwrite";
	}
	//草稿箱保存草稿
	@RequestMapping(value="draftWriter",method=RequestMethod.POST)
	public String draftWriter(Mail mail,MailBox mailBox) {
		mailService.update(mail, mailBox);
		return "redirect:drafts.do";
	}
	//草稿箱发送
	@RequestMapping(value="draftSendWriter",method=RequestMethod.POST)
	public String draftSendWriter(Mail mail,MailBox mailBox) {
		mailService.updateDraft(mail, mailBox);
		return "redirect:sendMail.do";
	}
	//加入回收站
	@RequestMapping(value="addtrash")
	public String addTrash(HttpServletRequest request) {
		Integer id=Integer.valueOf(request.getParameter("id"));
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		Trash trash=new Trash();
		trash.setUserId(user.getId());
		MailBox mailBox=boxService.getMailBoxById(id);
		trashService.insert(trash, mailBox);
		return "redirect:trash.do";
	}
	//回收站删除
	@RequestMapping(value="del")
	public String del(Trash trash) {
		trashService.delete(trash.getId());
		return "redirect:trash.do";
	}
}
